package com.gome.exception;

import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import lombok.NoArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
@RestControllerAdvice
@NoArgsConstructor
public class GlobalException extends RuntimeException {

    private  final Logger logger=Logger.getLogger(this.getClass());



    public GlobalException(String errCode,Object... params) {

        super(GlobalExceptionDatas.getErrMsg(errCode,params));
    }

    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
        logger.info( e.getMessage(), e);
        if(e instanceof SessionException) return new Result(ResultCode.UNAUTHORIZED, e.getMessage(), null);

        return  new Result(ResultCode.FAIL, e.getMessage(), null);
    }

}
